package com.xxadmin.usercenter.controller;

import com.xxadmin.common.dto.PageInfoDTO;
import com.xxadmin.common.util.clone.CopyUtils;
import com.xxadmin.common.util.page.PageUtils;
import com.xxadmin.common.validate.ValidGroup1;
import com.xxadmin.common.validate.ValidGroup2;
import com.xxadmin.common.vo.PageInfoVO;
import com.xxadmin.common.vo.ResponseModel;
import com.xxadmin.common.vo.ResponseModelBuilder;
import com.xxadmin.common.web.BaseController;
import com.xxadmin.usercenter.domain.RoleDetail;
import com.xxadmin.usercenter.dto.RoleDetailDTO;
import com.xxadmin.usercenter.dto.RoleDetailQueryDTO;
import com.xxadmin.usercenter.service.RoleDetailService;
import com.xxadmin.usercenter.vo.request.RoleDetailQueryVO;
import com.xxadmin.usercenter.vo.request.RoleDetailVO;
import com.xxadmin.usercenter.vo.response.RoleDetailResponseVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

/**
 * 角色
 *
 * @author:chenming
 * @date:2018/9/3
 */
@RestController
@RequestMapping("/role")
public class RoleDetailController extends BaseController{

    @Autowired
    private RoleDetailService roleDetailService;

    @PostMapping("/add")
    public ResponseModel addRoleDetail(@RequestBody @Validated(ValidGroup1.class) RoleDetailVO roleDetailVO){

        RoleDetailDTO roleDetailDTO = CopyUtils.copyBean(roleDetailVO,RoleDetailDTO.class);

        roleDetailService.addRoleDetail(roleDetailDTO);

        return ResponseModelBuilder.genericSuccessResponseModal();
    }

    @PutMapping("/update")
    public ResponseModel updateRoleDetail(@RequestBody @Validated(ValidGroup2.class)RoleDetailVO roleDetailVO){
        RoleDetailDTO roleDetailDTO = CopyUtils.copyBean(roleDetailVO,RoleDetailDTO.class);

        roleDetailService.updateRoleDetail(roleDetailDTO);

        return ResponseModelBuilder.genericSuccessResponseModal();
    }

    @GetMapping("/getRole/{id}")
    public ResponseModel<RoleDetailResponseVO> getRole(@PathVariable("id") Integer id){
        RoleDetailDTO roleDetail = roleDetailService.getRoleDetail(id);

        return ResponseModelBuilder.genericSuccessResponseModal(CopyUtils.copyBean(roleDetail,RoleDetailResponseVO
                .class));
    }

    @GetMapping("/list")
    public ResponseModel<PageInfoVO> listRoleDetail(RoleDetailQueryVO queryVO){

        RoleDetailQueryDTO queryDTO = CopyUtils.copyBean(queryVO,RoleDetailQueryDTO.class);

        PageInfoDTO<RoleDetailDTO> pageInfo = roleDetailService.listRoleDetail(queryDTO);

        return ResponseModelBuilder.genericSuccessResponseModal(PageUtils.getPageInfoVO(pageInfo,
                RoleDetailResponseVO.class));
    }

}
